﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Control;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FastCtr
{
    public enum UserRole
    {

        操作员 = 0,
        技师 = 1,
        工程师 = 2,
        None = 3
    }

    public partial class LogIn : Form
    {
        UserRole _userRole = UserRole.None;
        string _username = "-";
        static LogIn admit = new LogIn();
        public static event Action<UserRole> OnLogSuccessed;
        DirectoryInfo di = FastData.SystemPath.strDirPath;
        public static UserRole UserRole
        {
            get
            {
                return admit._userRole;
            }
        }

        public static string UserName
        {
            get
            {
                return admit._username;
            }
        }

        private LogIn()
        {
            InitializeComponent();
        }

        public static LogIn GetThis()
        {
            return admit;
        }



        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (_userRole == UserRole.工程师)
            {
                Regist userRegist = new Regist();
                userRegist.ShowDialog();
            }
            else
            {
                MessageBox.Show("请获取ME权限！", "message");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text) || string.IsNullOrWhiteSpace(textBox2.Text))
            {
                MessageBox.Show("输入信息不能为空！", "message");
                return;
            }
            string mima = FastData.SaveStatic.ReadIni(di + "\\ConfigurationData\\useradmit.ini", textBox1.Text, "密码");
            string a = di.FullName + "\\ConfigurationData\\useradmit.ini";
            if (mima != textBox2.Text)
            {
                MessageBox.Show("密码错误！", "message");
                return;
            }
            string userrole = FastData.SaveStatic.ReadIni(di + "\\ConfigurationData\\useradmit.ini", textBox1.Text, "角色");
            _userRole = (UserRole)Enum.Parse(typeof(UserRole), userrole);
            _username = textBox1.Text;
            textBox2.Text = "";
            Parent.Controls.Clear();
            OnLogSuccessed?.Invoke(_userRole);
        }
    }
}
